package com.yugao.fintech.antelope.shortlink.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yugao.fintech.antelope.base.ram.ActionType;
import com.yugao.fintech.antelope.base.ram.RamResource;
import com.yugao.fintech.antelope.shortlink.model.entity.LinkGroup;
import com.yugao.fintech.antelope.shortlink.model.rqrs.LinkGroupListReq;
import com.yugao.fintech.antelope.shortlink.model.rqrs.LinkGroupPageReq;
import com.yugao.fintech.antelope.shortlink.model.rqrs.LinkGroupReq;
import com.yugao.fintech.antelope.shortlink.service.LinkGroupService;
import com.yugao.fintech.framework.core.response.Resp;
import com.yugao.fintech.framework.validation.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@Api(tags = "短链组")
@RestController
@RequestMapping("/linkGroup")
public class LinkGroupController {
    @Autowired
    private LinkGroupService linkGroupService;

    @ApiOperation(value = "分页-短链组")
    @GetMapping("/pageGroup")
    @RamResource(action = "sl:linkGroup:query", name = "查询短链组", type = ActionType.READ)
    public Resp<IPage<LinkGroup>> pageGroup(@Validated LinkGroupPageReq req) {
        return Resp.ok(linkGroupService.pageGroup(req));
    }

    @ApiOperation("查询")
    @GetMapping("/listGroup")
    @RamResource(action = "sl:linkGroup:query", name = "查询短链组", type = ActionType.READ)
    public Resp<List<LinkGroup>> listGroup(@Validated LinkGroupListReq req) {
        return Resp.ok(linkGroupService.listGroup(req));
    }

    @ApiOperation("通过id获取")
    @GetMapping("/getGroup")
    @RamResource(action = "sl:linkGroup:query", name = "查询短链组", type = ActionType.READ)
    public Resp<LinkGroup> getGroup(@RequestParam(value = "id") Long id) {
        return Resp.ok(linkGroupService.getById(id));
    }

    @ApiOperation("保存")
    @RamResource(action = "sl:linkGroup:save", name = "保存短链组", type = ActionType.WRITE)
    @PostMapping("/saveGroup")
    public Resp<?> saveGroup(@Validated @RequestBody LinkGroupReq rq) {
        return Resp.ok(linkGroupService.saveGroup(rq));
    }

    @ApiOperation("更新")
    @PostMapping("/updateGroup")
    @RamResource(action = "sl:linkGroup:update", name = "更新短链组", type = ActionType.WRITE)
    public Resp<?> updateById(@Validated(UpdateGroup.class) @RequestBody LinkGroupReq rq) {
        return Resp.ok(linkGroupService.updateGroup(rq));
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batchDeleteGroup")
    @RamResource(action = "sl:linkGroup:delete", name = "删除短链组", type = ActionType.WRITE)
    public Resp<?> batchDeleteGroup(@RequestParam(value = "ids") List<Long> ids) {
        return Resp.ok(linkGroupService.deleteByIds(ids));
    }

    @ApiOperation("删除")
    @DeleteMapping("/deleteGroup")
    @RamResource(action = "sl:linkGroup:delete", name = "删除短链组", type = ActionType.WRITE)
    public Resp<?> delete(@RequestParam(value = "id") Long id) {
        return Resp.ok(linkGroupService.deleteByIds(Collections.singletonList(id)));
    }
}
